package edu.rpi.bt.client;

import java.util.Iterator;
import java.util.List;

import com.apress.progwt.client.college.ServiceCache;
import com.apress.progwt.client.domain.User;
import com.apress.progwt.client.rpc.StdAsyncCallback;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.map.LatLonPoint;

import edu.rpi.bt.client.domain.Comment;
import edu.rpi.bt.client.domain.Destination;

/**
 * 
 * @author Nate
 * 
 *         Base class to handle the Panels associated with each location
 *         marker on the map.
 */
abstract public class LocationPanel extends Panel {
    private Object marker;
    private String locationName;
    private String description;
    
    private LatLonPoint latlon;
    
    protected long destId;
    
    protected Destination destination;
    protected List<Comment> comments;
    //protected String[] imageUrls;
    protected List<String> imageUrls;
    protected long locationId;
    protected ServiceCache serviceCache;

    protected User thisUser;
    
    /*
     * Constructors
     */
    LocationPanel() {
        init();
    }

    LocationPanel(LatLonPoint ll) {
        latlon = ll;
        init();
    }

    LocationPanel(double lat, double lon) {
        latlon = new LatLonPoint(lat, lon);
        init();
    }
    
    private void init() {
        setBorder(false);
    }

    /*
     * If we only pass in the DestinationId, then get the Destination object here
     * Usually, though, we should instantiate with the Destination object
     */
    void loadData( long locationId ) {
        // Call to get whole thing here, based on locationId

        // Dummy data
        destination = new Destination();
        destination.setDescription("This is the description of the location in general. Quisque nec tellus. In sit amet eros nec libero condimentum ultrices. Duis eu nisl. Sed posuere, ante eu facilisis posuere, tortor quam sollicitudin neque, accumsan ullamcorper elit ligula eu erat.");
        destination.setName("The Location Name Goes Here");
    }

    
    protected void makeServiceCalls(){
        this.destId=destination.getId();  //use this to set the locationId which is used to fetch comments

        
        serviceCache.getCurrentUser(new StdAsyncCallback<User>("Get User") {

            public void onSuccess(User result) {
                super.onSuccess(result);
                if (result == null) {
                    //MessageBox.alert("user is none" );
                } else {
                    //MessageBox.alert("user IS " + result.getNickname());
                    load(result);
                }
            }

            public void onFailure(Throwable caught) {
                super.onFailure(caught);
              
            }
        });
        
        serviceCache.getAllCommentsForLocation(destId, new StdAsyncCallback<List<Comment>>("Get comments"){
            public void onSuccess(List<Comment> result) {
                super.onSuccess(result);
                if (result == null) {
                    //MessageBox.alert("result for location null"); 
                } else {
                    //MessageBox.alert("load location result"); 
                    load(result);
                    setupComments();
                    
                }
            }
          
            public void onFailure(Throwable caught) {
                super.onFailure(caught);
              
            }
        });

 
        serviceCache.getImages(destination.getName().replaceAll(" +", " OR ") , 4, new StdAsyncCallback<List<String>>("Get images"){
            public void onSuccess(List<String> result) {
                super.onSuccess(result);
                if (result == null) {
                    MessageBox.alert("result for images null"); 
                } else {
              //      MessageBox.alert("load location result"); 
                    loads(result);
                    setupImages();
                }
            }
          
            public void onFailure(Throwable caught) {
                super.onFailure(caught);
              
            }
        });        
    }

    
    
    public void load(List<Comment> comments){
       this.comments=comments;
    }
       
    public void load(User user){
       this.thisUser=user;
    }   
   
    public void loads(List<String> imgs){
       this.imageUrls=imgs;
    }   

    protected void setupComments(){
        
    }
    
    protected void setupImages(){
        
    }   
    /*
     * Getters
     */
    public Object getMarker() {
        return marker;
    }

    public String getLocationName() {
        return destination.getName();
    }

    public String getDescription() {
        return destination.getDescription();
    }

    protected String getImageTags( int maxTags ){
        String html="";
        String[] urls = this.getImageUrls(1);
        
        for(int i=0; i<urls.length && i < maxTags; i++){
            html += "<a href=\""+ urls[i] +"\" border=\"0\" class=\"locimage\" target=\"_blank\" ><img src=\"" + urls[i] + "\" class=\"flickrimg\"></a>";
        }
        
        return html;
    }
        
    
    public String[] getImageUrls( int stat) {
        String[] urls = {};

        if(stat==1){
        for (Iterator it = imageUrls.iterator(); it.hasNext(); ) {
            urls[urls.length] = (String) it.next();
         }
        } else {
            String[] urlsk = {"http://edition.cnn.com/2000/LAW/08/17/bailey.battles.02/map.florida.orlando.jpg",
                "http://peacecorpsonline.org/messages/jpeg/vermont2aa.jpg",
                "http://static.howstuffworks.com/gif/travel/properties/494/san-francisco-city-guide-ga-1-1.jpg"
        };
            urls = urlsk;
        }
        return urls;
    }

    public LatLonPoint getLatLon() {
        return latlon;
    }

    public String[][] getComments() {
        //MessageBox.alert("getting comments");
        String[][] tmp = new String[][]{};
        Comment c;
        
        for (Iterator it = comments.iterator(); it.hasNext(); ) {
            c = (Comment)it.next();
            tmp[tmp.length] = new String[] {
                    c.getComment()
            };
         }
        
  
        if( 1 == 1){
            return tmp;
        }
        
        String[][] comments = {
                new String[]{"Donec sed tellus ac nisi pellentesque volutpat. Curabitur non justo quis metus facilisis pharetra. Morbi elit. Ut et erat. Vestibulum sed lorem. Duis eget dolor. Maecenas quis nisi. Vivamus mattis accumsan mauris. Quisque cursus ipsum eget turpis semper porttitor. Fusce luctus consequat elit. Phasellus iaculis nisl ut purus. Vestibulum pellentesque, ligula quis vehicula hendrerit, urna nulla ullamcorper nibh, quis viverra felis leo eget erat. Sed facilisis vestibulum enim.", "df"} ,
                new String[]{"Nunc ullamcorper adipiscing magna. Morbi accumsan mi eu eros.",""},
                new String[]{"Nulla porta interdum nulla. Aenean auctor.",""},
                new String[]{"Nulla eu massa. Cras aliquam augue. Nam tempus ante vitae felis ullamcorper semper. In posuere condimentum lacus.",""},
                new String[]{"Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum vitae libero sit amet odio tempus facilisis. Praesent lobortis, ante vitae porttitor fermentum, justo enim varius lorem, sit amet semper risus mauris vitae sapien.",""},
                new String[]{"Aenean euismod. Donec vulputate rhoncus turpis. Maecenas tellus est, bibendum eget, ultricies elementum, tempus ut, nulla. ",""}
        };
        return comments;
    }

}
